package com.hustcad.plm.pdm.user.controller;


import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.hustcad.plm.pdm.user.service.PlmPartService;
import com.jsyxdl.k3cloud.listener.K3CloudHttpService;
import com.ty.basic.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/rest/v1/plmPart")
public class PlmPartOptionController {

    @Autowired
    PlmPartService plmPartService;



    @PostMapping("/add")
    public ResponseResult add(@RequestBody Map<String,Object> map) {
        String uuid= UUID.randomUUID().toString();
        map.put("id", uuid);
        plmPartService.add(map);
        return ResponseResult.success();
    }
    @PostMapping("/addPlmSourceType")
    public ResponseResult addPlmSourceType(@RequestBody Map<String,Object> map) {
        String uuid=UUID.randomUUID().toString();
        map.put("id", uuid);
        plmPartService.addPlmSourceType(map);
        return ResponseResult.success();
    }
    @PostMapping("/list")
    public ResponseResult<PageInfo<Map<String,Object>>> list(@RequestBody Map<String,Object> map) {
        PageMethod.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"), true);
        List<Map<String,Object>> list = plmPartService.list(map);
        PageInfo<Map<String,Object>> result = new PageInfo(list);
        return ResponseResult.success(result);
    }
    @PostMapping("/getPlmSourceType")
    public ResponseResult<PageInfo<Map<String,Object>>> getPlmSourceType(@RequestBody Map<String,Object> map) {
        PageMethod.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"), true);
        List<Map<String,Object>> list = plmPartService.getPlmSourceType(map);
        PageInfo<Map<String,Object>> result = new PageInfo(list);
        return ResponseResult.success(result);
    }
    @PostMapping("/edit")
    public ResponseResult edit(@RequestBody Map<String,Object> map) {
        plmPartService.edit(map);
        return ResponseResult.success();
    }
    @PostMapping("/editPlmSourceType")
    public ResponseResult editPlmSourceType(@RequestBody Map<String,Object> map) {
        plmPartService.editPlmSourceType(map);
        return ResponseResult.success();
    }


    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody Map<String,Object> map) {
        plmPartService.delete(map);
        return ResponseResult.success();
    }
    @PostMapping("/deletePlmSourceType")
    public ResponseResult deletePlmSourceType(@RequestBody Map<String,Object> map) {
        plmPartService.deletePlmSourceType(map);
        return ResponseResult.success();
    }
    @PostMapping("/json")
    public ResponseResult json(@RequestBody Map<String,Object> map) {
        try {
            K3CloudHttpService k3CloudHttpService= SpringUtil.getBean(K3CloudHttpService.class);
            if(k3CloudHttpService!=null){
                List<Map<String,Object>> list=k3CloudHttpService.jsonObjectToList((String) map.get("textarea"),(String) map.get("analysis"));
                return ResponseResult.success(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail("-1", e.getMessage());
        }
        return ResponseResult.fail("0","");
    }
    @PostMapping("/valuationType")
    public ResponseResult  valuationType() {
        List<Map<String,Object>> list = plmPartService.plmPartValuationType(null);
        return ResponseResult.success(list);
    }
    @PostMapping("/dataType")
    public ResponseResult dataType() {
        List<Map<String,Object>> list = plmPartService.plmPartDataType(null);
        return ResponseResult.success(list);
    }
    @PostMapping("/submitInPlmPart")
    public ResponseResult submitInPlmPart(@RequestBody Map<String,Object> map) {
        K3CloudHttpService k3CloudHttpService= SpringUtil.getBean(K3CloudHttpService.class);
        List<Map<String,Object>> list=k3CloudHttpService.inPlmPartToList((List<Map<String, Object>>) map.get("list"));
        plmPartService.submitInPlmPart(map,list);
        return ResponseResult.success();
    }

    @PostMapping("/getInPlmPart")
    public ResponseResult getInPlmPart(@RequestBody Map<String,Object> map) {
        List<Map<String, Object>> list=plmPartService.getInPartOption(map);
        return ResponseResult.success(list);
    }

    @PostMapping("/submitOutPlmPart")
    public ResponseResult submitOutPlmPart(@RequestBody Map<String,Object> map) {
        K3CloudHttpService k3CloudHttpService= SpringUtil.getBean(K3CloudHttpService.class);
        List<Map<String,Object>> list=k3CloudHttpService.inPlmPartToList((List<Map<String, Object>>) map.get("list"));

        plmPartService.submitOutPlmPart(map,list);
        return ResponseResult.success();
    }

    @PostMapping("/getOutPlmPart")
    public ResponseResult getOutPlmPart(@RequestBody Map<String,Object> map) {
        List<Map<String, Object>> list=plmPartService.getOutPartOption(map);
        return ResponseResult.success(list);
    }

    @PostMapping("/setPlmPartLink")
    public ResponseResult setPlmPartLink(@RequestBody Map<String,Object> map) {
        plmPartService.setPlmPartLink(map);
        return ResponseResult.success();
    }
    @PostMapping("/getPlmPartLink")
    public ResponseResult getPlmPartLink(@RequestBody Map<String,Object> map) {
        List<Map<String,Object>> list = plmPartService.getPlmPartLink(map);
        return ResponseResult.success(list);
    }
    @PostMapping("/getAllInPlmPart")
    public ResponseResult getAllInPlmPart(@RequestBody Map<String,Object> map) {
        try {
            K3CloudHttpService k3CloudHttpService=SpringUtil.getBean(K3CloudHttpService.class);
            if(k3CloudHttpService!=null){
                List<Map<String,Object>> list = plmPartService.getPlmPartLink(map);
                Map<String,Object> select=new HashMap<>();
                List<Map<String,Object>> resultList=new ArrayList<>();
                for(int i=0;i<list.size();i++){
                    Map<String,Object> resultMap=new HashMap<>();
                    resultMap.put("label",list.get(i).get("label"));
                    resultMap.put("value",list.get(i).get("plm_source_type_id"));
                    select.put("id",list.get(i).get("plm_source_type_id"));
                    List<Map<String,Object>> inList=plmPartService.getInPartOption(select);
                    if(inList.size()>0){
                        List<Map<String,Object>> children=new ArrayList<>();
                        resultMap.put("children",children);
                        k3CloudHttpService.settingPlmPartToChildren(inList,children,"0");
                    }
                    resultList.add(resultMap);
                }

                for(int i=0;i<resultList.size();i++){
                    k3CloudHttpService.settingPlmPartChildrenToNull(resultList);
                }

                return ResponseResult.success(resultList);
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail("-1", e.getMessage());
        }
        return ResponseResult.fail("0","");
    }

    @PostMapping("/editInPartOptionById")
    public ResponseResult editInPartOptionById(@RequestBody Map<String,Object> map) {
        plmPartService.editInPartOptionById(map);
        return ResponseResult.success();
    }
    @PostMapping("/addInPartOptionById")
    public ResponseResult addInPartOptionById(@RequestBody Map<String,Object> map) {
        map.put("node_key",map.get("id"));
        plmPartService.addInPartOptionById(map);
        return ResponseResult.success();
    }
    @PostMapping("/deleteInPartOptionById")
    public ResponseResult deleteInPartOptionById(@RequestBody Map<String,Object> map) {
        plmPartService.deleteInPartOptionById(map);
        return ResponseResult.success();
    }

    @PostMapping("/addOutPartOptionById")
    public ResponseResult addOutPartOptionById(@RequestBody Map<String,Object> map) {
        plmPartService.addOutPartOptionById(map);
        return ResponseResult.success();
    }
    @PostMapping("/deleteOutPartOptionById")
    public ResponseResult deleteOutPartOptionById(@RequestBody Map<String,Object> map) {
        plmPartService.deleteOutPartOptionById(map);
        return ResponseResult.success();
    }
}
